package com.xfcycle.zebrabikegp.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 自定义ToggleButton
 * Created by xFcycle on 2017/5/2.
 */

public class ToggleButton extends View {

    /**
     * 创建枚举类
     */
    public enum ToggleState {
        ON, OFF
    }

    private ToggleState toggleState;

    private Bitmap slideButton;
    private Bitmap switchBackGroundOn;
    private Bitmap switchBackGroundOff;
    private int eventX;

    private boolean isSlide = false;

    //重写两个构造器
    public ToggleButton(Context context) {
        super(context);
    }

    //第二个参数为布局文件中所有属性的key值和Value值的集合
    public ToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 给滑动按钮赋值
     */
    public void setSlideButton(int resId) {
        this.slideButton = BitmapFactory.decodeResource(this.getResources(), resId);
    }

    /**
     * 给打开背景赋值
     */
    public void setSwitchBackGroundOn(int resId) {
        this.switchBackGroundOn = BitmapFactory.decodeResource(this.getResources(), resId);
    }

    /**
     * 给关闭背景赋值
     */
    public void setSwitchBackGroundOff(int resId) {
        this.switchBackGroundOff = BitmapFactory.decodeResource(this.getResources(), resId);
    }

    /**
     * 设置toggleButton状态
     */
    public void setToggleState(ToggleState toggleState) {
        this.toggleState = toggleState;
    }

    /**
     * 测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(this.switchBackGroundOn.getWidth(), this.switchBackGroundOn.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置ToggleButton背景状态
        if (ToggleState.ON == toggleState) {
            canvas.drawBitmap(switchBackGroundOn, 0, 0, null);
        }
        if (ToggleState.OFF == toggleState) {
            canvas.drawBitmap(switchBackGroundOff, 0, 0, null);
        }

        //设置ToggleButton的滑动按钮位置
        //如果按钮正在滑动中，则实时调整slideButton位置
        if (isSlide) {
            //设置左边距
            int left = eventX - slideButton.getWidth() / 2;
            //防止slide Button 滑动超过背景左边
            if (left < 0) {
                left = 0;
            }
            //防止slide Button 滑动超过背景右边
            if (left > switchBackGroundOn.getWidth() - slideButton.getWidth()) {
                left = switchBackGroundOn.getWidth() - slideButton.getWidth();
            }
            canvas.drawBitmap(slideButton, left, 0, null);
        }
        //如果按钮滑动完毕，调整滑动按钮的最终位置
        if (!isSlide) {
            if (ToggleState.ON == toggleState) {
                canvas.drawBitmap(slideButton, switchBackGroundOn.getWidth() - slideButton.getWidth(), 0, null);
            }
            if (ToggleState.OFF == toggleState) {
                canvas.drawBitmap(slideButton, 0, 0, null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        eventX = (int) event.getX();
        switch (event.getAction()) {
            //判断此时正在滑动状态，修改isSlide的值
            case MotionEvent.ACTION_MOVE:
                isSlide = true;
                break;
            //当手离开屏幕后，判断此时滑动按钮是处于on还是off状态
            case MotionEvent.ACTION_UP:
                isSlide = false;
                int centerX = switchBackGroundOn.getWidth() / 2;

                if (eventX > centerX) {
                    if (toggleState != ToggleState.ON) {
                        toggleState = ToggleState.ON;
                        //将当前状态传给监听器，方便外部调用ToggleButton监听器获得当前状态
                        if (this.listener != null) {
                            this.listener.onToggleStatechange(this.toggleState);
                        }
                    }
                }

                if (eventX <= centerX) {
                    if (toggleState != ToggleState.OFF) {
                        toggleState = ToggleState.OFF;
                        if (this.listener != null) {
                            this.listener.onToggleStatechange(this.toggleState);
                        }
                    }
                }
                break;
        }
        invalidate();
        return true;
    }

    private OnToggleStateChangeListener listener;

    public void setOnToggleStateChangeListener(OnToggleStateChangeListener listener) {
        this.listener = listener;
    }

    public interface OnToggleStateChangeListener{
        void onToggleStatechange(ToggleState state);
    }
}
